import allure
from requests import Response

class ResponseAssertions:
    @staticmethod
    @allure.step('验证码')
    def assert_status_code(response:Response,expected_code:int):
        assert response.status_code == expected_code,f"状态码错误，期望：{expected_code},实际{response.status_code}"

    @staticmethod
    @allure.step('验证响应体字段')
    def assert_response_field(response:Response,field_path:str,expected_value):
        actual_value = response.json()
        for key in field_path.split('.'):
            actual_value = actual_value.get(key)
        assert actual_value == expected_value,f'字段错误，期望{expected_value},实际：{actual_value}'

    @staticmethod
    @allure.step('验证响应体字段')
    def assert_response_time(response:Response,max_time:float):
        assert response.elapsed.total_seconds()<=max_time,f'响应时间超过阈值·期望：{max_time}秒，实际：{response.elapsed.total_seconds()}秒'

